package com.easemob.helpdesk.widget.popupwindow;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.housekeeperapp.R;
import com.easemob.helpdesk.utils.CommonUtils;

/**
 * Created by liyuzhao on 16/3/1.
 */
public class SessionCloseWindow extends BasePopupWindow {
    private Context mContext;
    private View mTransferButton;


    public SessionCloseWindow(Context context) {
        this.mContext = context;
        @SuppressLint("InflateParams") View contentView = LayoutInflater.from(context).inflate(R.layout.popup_session_more, null);
        mTransferButton = contentView.findViewById(R.id.ll_transfer_session);
        this.setContentView(contentView);
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);

        if(Build.VERSION.SDK_INT >= 24){
            RelativeLayout layout = (RelativeLayout) contentView.findViewById(R.id.populayout);
            ((RelativeLayout.LayoutParams) layout.getLayoutParams()).topMargin = 95;
        }

        this.setFocusable(true);
        setOutsideTouchable(true);
        this.update();
        ColorDrawable cDraw = new ColorDrawable(Color.argb(0, 0, 0, 0));
        this.setBackgroundDrawable(cDraw);

    }

//    @Override
//    public void showAsDropDown(View anchor) {
//        if(Build.VERSION.SDK_INT >= 24){
//            Rect visibleFrame = new Rect();
//            anchor.getGlobalVisibleRect(visibleFrame);
//            int height = anchor.getResources().getDisplayMetrics().heightPixels - visibleFrame.bottom;
//            setHeight(height);
//        }
//
//        super.showAsDropDown(anchor);
//    }


    public View getmTransferButton() {
        return mTransferButton;
    }

    public void showPopupWindow(View parent) {
        if (!this.isShowing()) {

            try{
                this.showAsDropDown(parent, CommonUtils.convertDip2Px(mContext, -10), CommonUtils.convertDip2Px(mContext, -15));

//                if (Build.VERSION.SDK_INT < 24) {
//
//                } else {
//                    Rect visibleFrame = new Rect();
//                    parent.getGlobalVisibleRect(visibleFrame);
//                    int height = parent.getResources().getDisplayMetrics().heightPixels - visibleFrame.bottom;
//                    setHeight(height);
//
//                    int[] a = new int[2];
//                    parent.getLocationInWindow(a);
//                    this.showAtLocation(((Activity)mContext).getWindow().getDecorView(), Gravity.NO_GRAVITY, a[0] + CommonUtils.convertDip2Px(mContext, -10), parent.getHeight()+a[1]+15);
//                    this.update();
//                }

            }catch (IllegalStateException e){
                this.dismiss();
            }
        } else {
            this.dismiss();
        }
    }


}
